package pl.devfest.task;

import java.util.List;
import java.util.Random;

import pl.devfest.R;
import pl.devfest.model.Task;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TaskAdapter extends ArrayAdapter<Task> {

	private LayoutInflater infalter;
	private final List<Task> objects;
	private Random r;

	public TaskAdapter(Context context, List<Task> objects) {
		super(context, R.id.type, objects);
		this.objects = objects;
		infalter = LayoutInflater.from(context);
		r = new Random();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = infalter.inflate(R.layout.task_list_element, null);

		Task task = objects.get(position);

		if (task != null) {

			View lateBar = convertView.findViewById(R.id.lateBar);
			TextView type = (TextView) convertView.findViewById(R.id.type);
			TextView recipient = (TextView) convertView
					.findViewById(R.id.recipient);
			TextView description = (TextView) convertView
					.findViewById(R.id.description);
			ImageView priority = (ImageView) convertView
					.findViewById(R.id.priority);

			type.setText(task.getType());
			recipient.setText(task.getRecipient());
			description.setText(task.getRecipient());
			
			if(r.nextInt(2) == 0) {
				lateBar.setVisibility(View.INVISIBLE);
			} else {
				lateBar.setVisibility(View.VISIBLE);
			}

			switch (task.getPriority()) {
			case -1:
				priority.setImageResource(R.drawable.priority_low_1);
				break;

			case 0:
				priority.setImageResource(R.drawable.priority_normal);
				break;

			case 1:
				priority.setImageResource(R.drawable.priority_1);
				break;

			default:
				break;
			}
		}

		return convertView;
	}
}
